package ricardotavares.util.criptografia;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Encrypter {

    private static final String METODO_ENCRIPTACAO = "AES";
    private static final byte[] CHAVE = {85, 10, 0, -25, 68, 88, 46, 37, 107, 48, 10, -1, -37, -90, 70, -36};

    public static String encriptar(String value) throws EncryptorException {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(CHAVE, METODO_ENCRIPTACAO);

            Cipher cipher = Cipher.getInstance(METODO_ENCRIPTACAO);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(value.getBytes());

            return new BASE64Encoder().encode(encrypted);
        } catch (Exception e) {
            throw new EncryptorException("Erro ao criptografar informações " + e.getMessage());
        }
    }

    public static String decriptar(String encrypted) throws EncryptorException {

        byte[] decrypted = null;

        try {
            SecretKeySpec skeySpec = new SecretKeySpec(CHAVE, METODO_ENCRIPTACAO);

            byte[] decoded = new BASE64Decoder().decodeBuffer(encrypted);

            Cipher cipher = Cipher.getInstance(METODO_ENCRIPTACAO);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            decrypted = cipher.doFinal(decoded);
        } catch (Exception e) {
            throw new EncryptorException("Erro ao descriptografar informações " + e.getMessage());
        }

        return new String(decrypted);
    }
}